<template>
  <div class="com-container" @dblclick="backChina">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { getProvinceMapInfo } from '@/utils/map_utils'
import axios from 'axios'
export default {
  data() {
    return {
      // chart实例
      chartInstance: null,
      //   图标数据
      allData: null,
    }
  },
  mounted() {
    this.getScatterData()
    this.chartInit()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  methods: {
    async chartInit() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, 'chalk')
      const { data: chinaMap } = await axios.get(
        'http://localhost:8080/static/map/china.json'
      )
      this.$echarts.registerMap('china', chinaMap)
      const initOption = {
        title: {
          text: '商家分布 单击进入 双击退出',
          left: 20,
          top: 20,
        },

        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          itemStyle: {
            areaColor: '#dfe6e9',
            boderColor: '#333',
          },
        },
      }
      this.chartInstance.setOption(initOption)

      // 点击进入省份地图
      this.chartInstance.on('click', async (arg) => {
        const proviceInfo = getProvinceMapInfo(arg.name)
        const { data: res } = await axios.get(
          'http://localhost:8080' + proviceInfo.path
        )
        this.$echarts.registerMap(proviceInfo.key, res)
        const changeMap = {
          geo: {
            map: proviceInfo.key,
          },
        }
        this.chartInstance.setOption(changeMap)
        this.getScatterData()
      })
    },

    // 获取散点图数据
    async getScatterData() {
      const { data: res } = await this.$http.get('/map')
      this.allData = res
      this.updateChart()
    },
    // 渲染图表
    updateChart() {
      // 图例
      const legendData = this.allData.map((el) => el.name)
      const seriesArr = this.allData.map((el) => {
        return {
          symbolSize: 10,
          type: 'effectScatter',
          coordinateSystem: 'geo',
          name: el.name,
          data: el.children,
          rippleEffect: {
            scale: 5,
            brushType: 'stroke',
          },
        }
      })
      const option = {
        legend: {
          data: legendData,
          //   top: '5%',
          left: '5%',
          bottom: '5%',
          orient: 'vertical',
        },
        series: seriesArr,
      }
      this.chartInstance.setOption(option)
    },

    // 监听窗口,适配屏幕
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      // 分辨率配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
        },
        legend: {
          textStyle: {
            fontSize: titleFontSize / 2,
          },
          itemStyle: {
            itemWidth: titleFontSize / 2,
            itemHeight: titleFontSize / 2,
            itemGap: titleFontSize / 2,
          },
        },
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },

    // 双击回到中国地图
    backChina() {
      this.chartInstance.setOption({
        geo: {
          map: 'china',
        },
      })
    },
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
  },
}
</script>

<style></style>
